<template>
	<el-container>
		<el-aside width="30%">
			<el-container>
				<el-header>
					<div class="right-panel">
						<div class="right-panel-search">
							<ProductTableSelect placeholder="请选择产品和路线" @change="change" />
						</div>
					</div>
				</el-header>
				<el-main class="nopadding">
					<scTable
						ref="proctable"
						:api-obj="this.$API.procroutingitem.pageByRoutingId"
						:params="bomReqParams"
						row-key="id"
						paginationLayout="prev, pager, next"
						:initData="false"
						highlightCurrentRow
						hideDo
						hideContextMenu
						@row-click="rowClick">
						<el-table-column label="#" type="index" align="center" width="40"></el-table-column>
						<el-table-column property="process.procCode" label="工序编号" align="center" width="120" />
						<el-table-column property="process.procName" label="工序名称" align="center" />
					</scTable>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button
						icon="el-icon-plus"
						type="primary"
						:disabled="!selectedValue.process.id"
						@click="open_dialog" />
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable
					ref="table"
					:api-obj="this.$API.procbom.pageByProcessId"
					:params="bomReqParams"
					:column="column"
					row-key="id"
					:initData="false"
					stripe
					hideDo
					hideContextMenu>
					<el-table-column label="#" type="index" width="40" />
					<el-table-column property="material.itemCode" label="物料编号" align="center" width="160" />
					<el-table-column property="material.itemName" label="物料名称" align="center" width="160" />
					<el-table-column property="qty" label="需求数量" align="center" width="100" />
					<el-table-column property="pos" label="上料顺序" align="center" width="100">
						<template #default="scope">
							<el-link style="color: #5cd29d" :underline="false" @click="upSort(scope.row)">
								<el-icon><el-icon-top /></el-icon>
							</el-link>
							<el-divider direction="vertical" />
							<el-link style="color: #5cd29d" :underline="false" @click="downSort(scope.row)">
								<el-icon><el-icon-bottom /></el-icon>
							</el-link>
						</template>
					</el-table-column>
					<el-table-column property="sourceRawOpCode" label="物料来源工位" align="center" />
					<el-table-column align="center" fixed="right" label="操作" width="140">
						<template #default="scope">
							<el-button text type="primary" size="small" @click="open_dialog(scope.row)">编辑</el-button>
							<el-divider direction="vertical" />
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</template>
					</el-table-column>
				</scTable>
			</el-main>

			<modify ref="modify" @complete="complete" />
		</el-container>
	</el-container>
</template>

<script>
import modify from "./modify";
import ProductTableSelect from "./components/ProductTableSelect";

export default {
	components: {
		modify,
		ProductTableSelect,
	},
	data() {
		return {
			bomReqParams: {
				productId: 0,
				routingId: 0,
				processId: 0,
			},
			selectedValue: {
				product: {},
				routing: {},
				process: {},
			},
			canOpenDialog: false,
		};
	},
	methods: {
		change(item) {
			this.selectedValue.product = item.product;
			this.selectedValue.routing = item.routing;
			this.selectedValue.process = {};

			this.bomReqParams.productId = this.selectedValue.product.id;
			this.bomReqParams.routingId = this.selectedValue.routing.id;

			this.$refs.proctable.refresh();
			this.$refs.table.refresh();
		},
		complete() {
			this.$refs.table.refresh();
		},
		rowClick(row) {
			this.selectedValue.process = row.process;
			this.bomReqParams.processId = this.selectedValue.process.id;
			this.$refs.table.refresh();
		},
		async upSort(row) {
			if (row.pos == 1) {
				return;
			}
			await this.$API.procbom.upsort.put(row);
			this.$refs.table.refresh();
		},
		async downSort(row) {
			await this.$API.procbom.downsort.put(row);
			this.$refs.table.refresh();
		},
		async table_del(row) {
			let resp = await this.$API.procbom.delete.delete(row.id);
			if (resp.code == 200) {
				this.$refs.table.refresh();
				this.$message.success("删除成功");
			} else {
				this.$alert(resp.message, "提示", { type: "error" });
			}
		},
		open_dialog(row) {
			if (row.id) {
				Object.assign(row, this.selectedValue);
				this.$refs.modify.open(row);
			} else {
				this.$refs.modify.open(this.selectedValue);
			}
		},
	},
};
</script>
